package com.xiaolei.baselib.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import com.xiaolei.baselib.annotation.AssetOpenInputStream;
import com.xiaolei.baselib.annotation.setContentView;
import com.xiaolei.baselib.util.AnnotationUtil;

public abstract class BaseActivity extends Activity
{
	private String TAG = "BaseActivity";
	private String className = this.getClass().getName();
	private Field fields[] = null;
	private AssetManager mAssetManager;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Log.i(TAG + ":onCreate", className);
		init();
	}

	private void init()
	{
		try
		{
			initContentAnnotation();
			initAnotationViews();
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} catch (InstantiationException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		initViews();
		setListeners();
		initDatas(getIntent());
		loadDatas();
	}
	
	private void initContentAnnotation()
	{
		Class<? extends Activity> class1 = this.getClass();
		if(class1.isAnnotationPresent(setContentView.class))
		{
			setContentView contentView = class1.getAnnotation(setContentView.class);
			int contentid = contentView.value();
			if(contentid != 0)
			{
				setContentView(contentid);
			}
		}
	}
	
	private void initAnotationViews() throws IllegalAccessException, InstantiationException, IOException
	{
		fields = this.getClass().getDeclaredFields();

		for (int i = 0; i < fields.length; i++)
		{
			Field field = fields[i];
			if (field.isAnnotationPresent(AssetOpenInputStream.class))// 是打开资源文件夹的注解
			{
				if (field.getType() == InputStream.class)
				{
					field.setAccessible(true);// 设置私有的 可访问属性 为true
					AssetOpenInputStream aso = field.getAnnotation(AssetOpenInputStream.class);
					String name = aso.value();
					if (mAssetManager == null)
					{
						mAssetManager = getAssets();
					}
					InputStream inputStream = mAssetManager.open(name);
					field.set(this, inputStream);
				}
			} else
			{
				Object object = AnnotationUtil.getAnnotationObject(this, field);
				field.setAccessible(true);// 设置私有的 可访问属性 为true
				if (object != null)
					field.set(this, object);
			}
		}
	}

	@Override
	protected void onResume()
	{
		Log.i(TAG + ":onResume", className);
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		Log.i(TAG+":onPause",className);
		super.onPause();
	}

	@Override
	protected void onDestroy()
	{
		Log.i(TAG + ":onDestroy", className);
		if (toast != null)
		{
			toast = null;
		}
		if (mAssetManager != null)
		{
			mAssetManager.close();
		}
		super.onDestroy();
	}

	/**
	 * 实例化界面
	 */
	public abstract void initViews();

	/**
	 * 设置监听事件
	 */
	public abstract void setListeners();

	/**
	 * 初始化数据
	 *
	 * @param intent
	 */
	public abstract void initDatas(Intent intent);

	/**
	 * 加载耗时数据
	 */
	public abstract void loadDatas();

	public void startActivity(Class<? extends Activity> activity)
	{
		startActivity(activity, null);
	}

	public void startActivity(Class<? extends Activity> activity, Intent intent)
	{
		Intent intent1 = intent == null ? new Intent() : new Intent(intent);
		intent1.setClass(this, activity);
		startActivity(intent1);
	}

	public <T extends View> T findViewByID(int id)
	{
		return (T) super.findViewById(id);
	}

	private Toast toast = null;

	protected void Alert(String msg)
	{
		if (toast == null)
		{
			toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
		}
		toast.setText(msg);
		toast.show();
	}

	/**
	 * 按下返回键
	 */
	protected void onKeyBackDown()
	{
	}
	protected boolean onKeyBackDown(int keyCode, KeyEvent event)
	{
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			onKeyBackDown();
			return onKeyBackDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}
}
